package com.ycz.sas.jgen.ui;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.ycz.sas.jgen.connection.LocalContextHolder;
import com.ycz.sas.jgen.domain.DBConfig;
import com.ycz.sas.jgen.handler.XMLParser;

public class DBConfigPanel extends JPanel{

	private JComboBox cbScheme;

	private JTextField txtUser;
	private JPasswordField txtPassword;
	private JTextField txtUrl;
	private JTextField txtPort;
	private JTextField txtDataBase;
	private List<DBConfig> dbConfigs;
	

	public DBConfigPanel(){
		super();
		dbConfigs = XMLParser.getDBConfigs();
		
		initLayout();
	}
	
	private void initLayout(){
		JLabel contUser = new JLabel("UserName");
		JLabel contPassword = new JLabel("Password");
		JLabel contUrl = new JLabel("Url              ");
		JLabel contPort = new JLabel("Port           ");
		JLabel contDataBase = new JLabel("DataBase");
		JLabel contScheme = new JLabel("Scheme");
		
		Vector<String> schemes = new Vector<String>();
		for (int i = 0; i < dbConfigs.size(); i++) {
			schemes.add(dbConfigs.get(i).getKey());
		}
		cbScheme = new JComboBox(schemes);
		
		txtUser = new JTextField("easlhdata", 30);
		txtPassword = new JPasswordField("tiss1e6a84", 30);
		txtUrl = new JTextField("172.16.12.32", 30);
		txtPort = new JTextField("1521", 30);
		txtDataBase = new JTextField("easdb", 30);
		
		cbScheme.setSelectedIndex(0);
		DBConfig defaultConfig = dbConfigs.get(0);
		updateComponentValue(defaultConfig);
		
		add(contUser);add(txtUser);
		add(contPassword);add(txtPassword);
		add(contUrl);add(txtUrl);
		add(contPort);add(txtPort);
		add(contDataBase);add(txtDataBase);
		add(contScheme);add(cbScheme);
		
		cbScheme.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent e) {
				int selectedIndex = cbScheme.getSelectedIndex();
				updateComponentValue(dbConfigs.get(selectedIndex));
			}

		});
	}
	
	private void updateComponentValue(DBConfig config){
		txtUrl.setText(config.getHost());
		txtPort.setText(config.getPort());
		txtUser.setText(config.getUser());
		txtPassword.setText(config.getPassword());
		txtDataBase.setText(config.getDatabase());
		LocalContextHolder.setCustomerType(config.getKey());
	}
	
	public String getConnectStr(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("jdbc:oracle:thin:@");
		buffer.append(txtUrl.getText());
		buffer.append(":");
		buffer.append(txtPort.getText());
		buffer.append(":");
		buffer.append(txtDataBase.getText());
		return buffer.toString();
	}
	
	public String getUser(){
		return txtUser.getText();
	}
	
	public String getPassword(){
		StringBuffer pwdBuffer = new StringBuffer();
		for(char c : txtPassword.getPassword()){
			pwdBuffer.append(c);
		}
		return pwdBuffer.toString();
	}

}
